<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/11/8
 * Time: 13:36
 */

namespace app\index\controller;
use app\index\base_controller\BaseController;
use app\index\report\LandForGGJ;
use app\index\service\ExcelService;
use app\index\table\BuildingTable;
use app\index\table\FacilityTable;
use app\index\table\FurnTable;
use app\index\table\LandTable;
use think\Db;
use think\Exception;

class Report extends BaseController{

    /**
     * page--设备资产导出上报国管局
     */
    public function facilityForGGJ(){
        $date = input('date');
        $type = input('type');
        $belong = input('belong');
        $this->assign('date',$date);
        $this->assign('belong',$belong);
        $this->assign('type',$type);
        return view('facility_for_ggj');
    }

    /**
     * action 国管局土地
     */
    public function getLandForGGJ(){
        $limit = input('limit');
        $offset = input('offset');
        $where = [];
        $data = LandForGGJ::getData($where,$limit,$offset);
        $list = $data[0];
        $total = $data[1];
        return ['rows'=>$list,'total'=>$total];
    }

    public function exportForGGJ(){
        $date = input('date');
        $type = input('type');
        $belong = input('belong');
        if($type == '1'){
            $data = LandForGGJ::getData(null,null,null);
            $list = $data[0];
            LandForGGJ::exportData($list);
        }
    }

    /**
     * page 导出国管局页面
     */
    public function exportGGJPage(){

        $endDate = input('endDate');
        $belong = input('belong');
        $type = input('type');
        $this->assign('endDate',$endDate);
        $this->assign('belong',$belong);
        $this->assign('type',$type);

        if($type == '1'){
            $condition[] = ['GET_DATE','<=',$endDate];
            $totalSql = LandTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }
        if($type == '2'){
            $condition[] = ['ENTERTIME','<=',$endDate];
            $condition[] = ['BELONG_ID','=',$belong];
            $totalSql = FacilityTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }
        if($type == '3'){
            $condition[] = ['ENTER_TIME','<=',$endDate];
            $condition[] = ['BELONG_ID','=',$belong];
            $totalSql = FurnTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }

        if($type == '4'){
            $condition[] = ['GET_DATE','<=',$endDate];
            $totalSql = BuildingTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }

        $start = 0;
        $step = 5000;
        $list = [];
        while (true){
            if($start + $step >= $total){
                $list[] = ['start'=>$start,'limit'=>$step,'end'=>$total];
                break;
            }
            else{
                $list[] = ['start'=>$start,'limit'=>$step,'end'=>$start + $step];
                $start += $step;
            }
        }
        $this->assign('list',$list);

        $this->view->engine->layout('layout_popup');
        return view('export');
    }


    /**
     * page--设备资产导出上报财政部
     */
    public function facilityForCZB(){
        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');
        $type = input('type');
        $this->assign('startDate',$startDate);
        $this->assign('endDate',$endDate);
        $this->assign('belong',$belong);
        $this->assign('type',$type);
        return view('facility_for_czb');
    }

    /**
     * page 导出财政部数据页面
     */
    public function exportCZBPage(){

        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');
        $type = input('type');
        $this->assign('startDate',$startDate);
        $this->assign('endDate',$endDate);
        $this->assign('belong',$belong);
        $this->assign('type',$type);

        if($type == '1'){
            $condition[] = ['GET_DATE','>=',$startDate];
            $condition[] = ['GET_DATE','<=',$endDate];
            $totalSql = LandTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }
        if($type == '2'){
            $condition[] = ['ENTERTIME','>=',$startDate];
            $condition[] = ['ENTERTIME','<=',$endDate];
            $condition[] = ['BELONG_ID','=',$belong];
            $totalSql = FacilityTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }
        if($type == '3'){
            $condition[] = ['ENTER_TIME','>=',$startDate];
            $condition[] = ['ENTER_TIME','<=',$endDate];
            $condition[] = ['BELONG_ID','=',$belong];
            $totalSql = FurnTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }

        if($type == '4'){
            $condition[] = ['GET_DATE','>=',$startDate];
            $condition[] = ['GET_DATE','<=',$endDate];
            $totalSql = BuildingTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            $total = $total[0]['TP_COUNT'];
            $this->assign('total',$total);
        }

        $start = 0;
        $step = 5000;
        $list = [];
        while (true){
            if($start + $step >= $total){
                $list[] = ['start'=>$start,'limit'=>$step,'end'=>$total];
                break;
            }
            else{
                $list[] = ['start'=>$start,'limit'=>$step,'end'=>$start + $step];
                $start += $step;
            }
        }
        $this->assign('list',$list);

        $this->view->engine->layout('layout_popup');
        return view('export');
    }

    /**
     * action 财政部土地数据
     */
    public function getLandForCZB(){

        $startDate = input('startDate');
        $endDate = input('endDate');

        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'GET_DATE desc';
        }

        $condition[] = ['GET_DATE','>=',$startDate];
        $condition[] = ['GET_DATE','<=',$endDate];

        try{
            $listSql = LandTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = LandTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return ['err'=>$e->getMessage(),'sql'=>Db::table('')->getLastSql()];
        }

    }

    /**
     * action 财政部设备数据
     */
    public function getFacilityForCZB(){
        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');

        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'ENTERTIME desc';
        }

        if(!empty($startDate)){
            $condition[] = ['ENTERTIME','>=',$startDate];
        }
        $condition[] = ['ENTERTIME','<=',$endDate];
        $condition[] = ['BELONG_ID','=',$belong];

        try{
            $listSql = FacilityTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = FacilityTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return ['err'=>$e->getMessage(),'sql'=>Db::table('')->getLastSql()];
        }
    }

    /**
     * action 财政部家具数据
     */
    public function getFurnForCZB(){
        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');

        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'ENTER_TIME desc';
        }

        if(!empty($startDate)){
            $condition[] = ['ENTER_TIME','>=',$startDate];
        }
        $condition[] = ['ENTER_TIME','<=',$endDate];
        $condition[] = ['BELONG_ID','=',$belong];

        try{
            $listSql = FurnTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = FurnTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return ['err'=>$e->getMessage(),'sql'=>Db::table('')->getLastSql()];
        }
    }


    /**
     * action 财政部房屋构筑物数据
     */
    public function getBuildingForCZB(){
        $startDate = input('startDate');
        $endDate = input('endDate');

        $limit = input('limit');
        $offset = input('offset');
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'GET_DATE desc';
        }

        if(!empty($startDate)){
            $condition[] = ['GET_DATE','>=',$startDate];
        }
        $condition[] = ['GET_DATE','<=',$endDate];

        try{
            $listSql = BuildingTable::table()->where($condition)->order($sortStr)->limit($offset,$limit)->fetchSql()->select();
            $list = Db::query($listSql);
            $totalSql = BuildingTable::table()->where($condition)->fetchSql()->count();
            $total = Db::query($totalSql);
            return ['rows'=>$list,'total'=>$total[0]['TP_COUNT']];
        }
        catch (Exception $e){
            return ['err'=>$e->getMessage(),'sql'=>Db::table('')->getLastSql()];
        }
    }

    /**
     * page 导出土地for财政部
     */
    public function exportLandForCZB(){

        $startDate = input('startDate');
        $endDate = input('endDate');

        $limit = input('limit');
        $offset = input('start');
        $sortStr = 'GET_DATE desc';

        if(!empty($startDate)){
            $condition[] = ['GET_DATE','>=',$startDate];
        }
        $condition[] = ['GET_DATE','<=',$endDate];

        try
        {
            $listSql = LandTable::table()->where($condition)->order($sortStr)->limit($offset, $limit)->fetchSql()->select();
            $list = Db::query($listSql);

            LandTable::exportExcel($list,'土地-财政部:'.($offset+1).'-'.($offset+$limit));
        }
        catch (Exception $e){
            return '数据库操作出错：'.$e->getMessage().'<br>'.Db::table('')->getLastSql();
        }

    }

    /**
     * page 导出设备for财政部
     */
    public function exportFacilityForCZB(){
        set_time_limit(120);
        ini_set('memory_limit','1024M');

        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');

        $limit = input('limit');
        $offset = input('start');
        $sortStr = 'ENTERTIME desc';

        if(!empty($startDate)){
            $condition[] = ['ENTERTIME','>=',$startDate];
        }
        $condition[] = ['ENTERTIME','<=',$endDate];
        $condition[] = ['BELONG_ID','=',$belong];
        try
        {
            $listSql = FacilityTable::table()->where($condition)->order($sortStr)->limit($offset, $limit)->fetchSql()->select();
            $list = Db::query($listSql);
            FacilityTable::exportExcel($list,'设备-财政部:'.($offset+1).'-'.($offset+$limit));
        }
        catch (Exception $e){
            return '数据库操作出错：'.$e->getMessage().'<br>'.Db::table('')->getLastSql();
        }

    }

    /**
     * page 导出家具for财政部
     */
    public function exportFurnForCZB(){
        set_time_limit(120);
        ini_set('memory_limit','1024M');

        $startDate = input('startDate');
        $endDate = input('endDate');
        $belong = input('belong');

        $limit = input('limit');
        $offset = input('start');
        $sortStr = 'ENTER_TIME desc';

        if(!empty($startDate)){
            $condition[] = ['ENTER_TIME','>=',$startDate];
        }
        $condition[] = ['ENTER_TIME','<=',$endDate];
        $condition[] = ['BELONG_ID','=',$belong];
        try
        {
            $listSql = FurnTable::table()->where($condition)->order($sortStr)->limit($offset, $limit)->fetchSql()->select();
            $list = Db::query($listSql);
            FurnTable::exportExcel($list,'家具-财政部:'.($offset+1).'-'.($offset+$limit));
        }
        catch (Exception $e){
            return '数据库操作出错：'.$e->getMessage().'<br>'.Db::table('')->getLastSql();
        }

    }

    /**
     * page 导出建筑for财政部
     */
    public function exportBuildingForCZB(){
        set_time_limit(120);
        ini_set('memory_limit','1024M');

        $startDate = input('startDate');
        $endDate = input('endDate');

        $limit = input('limit');
        $offset = input('start');
        $sortStr = 'GET_DATE desc';

        if(!empty($startDate)){
            $condition[] = ['GET_DATE','>=',$startDate];
        }
        $condition[] = ['GET_DATE','<=',$endDate];
        try
        {
            $listSql = BuildingTable::table()->where($condition)->order($sortStr)->limit($offset, $limit)->fetchSql()->select();
            $list = Db::query($listSql);
            BuildingTable::exportExcel($list,'房屋构筑物-财政部:'.($offset+1).'-'.($offset+$limit));
        }
        catch (Exception $e){
            return '数据库操作出错：'.$e->getMessage().'<br>'.Db::table('')->getLastSql();
        }
    }

}